package com.foot.travel.fragment;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.Toast;

import com.foot.travel.R;
import com.foot.travel.event.NetRefreshEvent;
import com.foot.travel.viewholder.SchoolTravelViewHolder;
import com.koalac.ironman.common.adapter.CommonAdapter;
import com.koalac.ironman.common.module.ModuleActivity.LoadState;
import com.koalac.ironman.net.MyRequest;
import com.koalac.ironman.net.ResponseCallback;
import com.koalac.ironman.net.pojo.request.ClientProtocolListBase;
import com.koalac.ironman.net.pojo.request.GetOutdoorList;
import com.koalac.ironman.net.pojo.response.BaseResponse;
import com.koalac.ironman.net.pojo.response.Response;
import com.koalac.ironman.net.pojo.response.SchoolTravelItem;
import com.koalac.ironman.widget.LoadingDialog;
import com.koalac.ironman.widget.PullToRefreshView;
import com.koalac.ironman.widget.PullToRefreshView.OnFooterRefreshListener;
import com.koalac.ironman.widget.PullToRefreshView.OnHeaderRefreshListener;
import com.shizhefei.fragment.LazyFragment;
import com.squareup.otto.Subscribe;

public class OutdoorListFragment extends LazyFragment implements
	OnHeaderRefreshListener, OnFooterRefreshListener, OnItemClickListener {

    PullToRefreshView mOutdoorContainer;
    ListView mOutdoorListView;

    public CommonAdapter<SchoolTravelItem> mSchoolTravelItemAdapter;
    private GetOutdoorList mGetOutdoorList;
    public List<SchoolTravelItem> mSchoolTravelItems = new ArrayList<SchoolTravelItem>();
    View mNetworkLayout;

    @Override
    public void onCreateViewLazy(Bundle savedInstanceState) {
	super.onCreateViewLazy(savedInstanceState);
	setContentView(R.layout.outdoor_list);
	mOutdoorContainer = (PullToRefreshView) findViewById(R.id.outdoor_container);
	mOutdoorListView = (ListView) findViewById(R.id.outdoor_listview);
	mNetworkLayout = findViewById(R.id.network_layout);
	initOutdoorList();
	mOutdoorContainer.setOnHeaderRefreshListener(this);
	mOutdoorContainer.setOnFooterRefreshListener(this);
	mOutdoorListView.setOnItemClickListener(this);
    }
    
    @Subscribe
    public void onRefresh(NetRefreshEvent netRefreshEvent) {
	mNetworkLayout.setVisibility(View.GONE);
	initOutdoorList();
    }
    

    public void initOutdoorList() {
	mGetOutdoorList = new GetOutdoorList();
	mSchoolTravelItemAdapter = new CommonAdapter<SchoolTravelItem>(
		SchoolTravelViewHolder.class, mSchoolTravelItems,
		getActivity(), R.layout.school_travel_item);
	mOutdoorListView.setAdapter(mSchoolTravelItemAdapter);
	loadData(SchoolTravelItem.class, LoadState.init, mGetOutdoorList,
		mOutdoorContainer, mSchoolTravelItemAdapter, mSchoolTravelItems);
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position,
	    long id) {
    }

    @Override
    public void onFooterRefresh(PullToRefreshView view) {
    }

    @Override
    public void onHeaderRefresh(PullToRefreshView view) {
    }

    protected int page = 1;
    protected final int perPage = 20;
    protected LoadingDialog p = null;

    /**
	 * 加载数据
	 */
	public <T extends BaseResponse> void loadData(final Class<T> clazz,
			final LoadState loadState, ClientProtocolListBase params,
			final PullToRefreshView container, final CommonAdapter<T> adapter,
			final List<T> list) {
		/**
		 * 无网络时的UI表现
		 */
		if (!MyRequest.checkNetWork(getActivity())) {
			Toast.makeText(getActivity(), "当前无网络,请检查网络后再试!", Toast.LENGTH_SHORT)
					.show();
			container.onHeaderRefreshComplete();
			container.onFooterRefreshComplete();
			mNetworkLayout.setVisibility(View.VISIBLE);
			return;
		}
		if (loadState == LoadState.init) {
			page = 0;
			p = LoadingDialog.createLoadingDialog(getActivity(), "", "获取户外拓展列表...");
		} else if (loadState == LoadState.refresh) {
			page = 0;
		} else if (loadState == LoadState.more) {

		}
		params.setPage(page);
		params.setPerpage(perPage);
		/**
		 * 保存到NetUIManager类中
		 */
		container.setTag(loadState);

		MyRequest.request(getActivity(), params, new ResponseCallback() {
			@Override
			public void process(boolean result, Context context, Response data,
					String msg) {
				Toast toast = null;
				if (result) {
					List<T> list = data.getList(clazz);
					if (list != null && list.size() > 0) {
						if (loadState == LoadState.init) {
							adapter.clear();
						} else if (loadState == LoadState.refresh) {
							adapter.clear();
						} else if (loadState == LoadState.more) {
						}
						page += 1;
						adapter.addAll(list);
						adapter.notifyDataSetChanged();
					} else {
						if (loadState == LoadState.init) {
							list.clear();
							p.cancel();
							com.koalac.ironman.util.ToastUtil.toast(getActivity(), "没有数据");
						} else if (loadState == LoadState.refresh) {
							list.clear();
							p.cancel();
							container.onHeaderRefreshComplete();
							com.koalac.ironman.util.ToastUtil.toast(getActivity(), "没有数据");
						} else if (loadState == LoadState.more) {
							p.cancel();
							container.onFooterRefreshComplete();
							com.koalac.ironman.util.ToastUtil.toast(getActivity(), "没有更多数据");
						}
					}
					mNetworkLayout.setVisibility(View.GONE);
				} else {
				    mNetworkLayout.setVisibility(View.VISIBLE);
					toast = Toast.makeText(context, "数据异常，请稍后再试", 3);
					toast.show();
				}
				if (loadState == LoadState.init) {
					p.cancel();
				} else if (loadState == LoadState.refresh) {
					container.onHeaderRefreshComplete();
				} else if (loadState == LoadState.more) {
					container.onFooterRefreshComplete();
				}
			}
		});
	}


}
